<template>
  <Card style="height:800px">
    <Form ref="formItem" :model="formItem" :label-width="100" :rules="formValid" inline>
      <FormItem label="状态:" style="margin-left:200px;" prop="status">
        <Select v-model="formItem.status" style="width:200px" :disabled="allMark">
          <Option v-for="item in statusList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </FormItem>
      <FormItem label="资金源属性:" style="margin-left:150px;" prop="fundSourceAttribute">
        <Select v-model="formItem.fundSourceAttribute" style="width:200px" :disabled="allMark">
          <Option v-for="item in fundSourceAttributeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </FormItem>
      <br>
      <FormItem label="资金源名称:" prop="fundName" style="margin-left:200px;">
        <Input placeholder="请输入" v-model="formItem.fundName" :disabled="allMark" style="width: 200px"/>
      </FormItem>
      <FormItem label="法人手机号码:" prop="fundSourceMobile" style="margin-left:150px;">
        <Input placeholder="请输入" v-model="formItem.fundSourceMobile" :disabled="allMark" style="width: 200px"/>
      </FormItem>
      <br>
      <FormItem label="法人身份证:" prop="fundSourceIcNo" style="margin-left:200px;">
        <Input placeholder="请输入" v-model="formItem.fundSourceIcNo" :disabled="allMark" style="width: 200px"/>
      </FormItem>
      <FormItem label="放款方式:" style="margin-left:150px;" prop="fundLoanType">
        <Select v-model="formItem.fundLoanType" style="width:200px" :disabled="allMark">
          <Option v-for="item in fundLoanTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </FormItem>
      <br>
      <FormItem label="银行列表:" style="margin-left:200px; width:55%">
        <CheckboxGroup v-model="formItem.bankCodeList" :disabled="allMark" style="margin-top:-10px;">
          <Checkbox style="margin-top:10px" :disabled="allMark" v-for="item in bankNameList" :key="item.bankCode" :label='item.bankCode'><span>{{item.bankName}}</span></Checkbox>
        </CheckboxGroup>
      </FormItem>
      <br>
      <FormItem prop="dayLimit" label="当日限额:" style="margin-left:200px;">
        <Input placeholder="请输入" v-model="formItem.dayLimit" :disabled="allMark" style="width: 200px"></Input>
      </FormItem>
      <FormItem prop="addMaxAmount" label="进件最大额度:" style="margin-left:150px;">
        <Input placeholder="请输入" v-model="formItem.addMaxAmount" :disabled="allMark" style="width: 200px"></Input>
      </FormItem>
      <br>
      <FormItem prop="minAmount" label="产品金额范围:" style="margin-left:200px;">
        <Input placeholder="请输入" v-model="formItem.minAmount" :disabled="allMark" style="width: 200px"></Input>
      </FormItem>
      <FormItem prop="maxAmount" label="至:" style="margin-left:-75px;">
        <Input placeholder="请输入" v-model="formItem.maxAmount" :disabled="allMark" style="width: 200px"></Input>
      </FormItem>
      <br>
      <FormItem prop="dayResAmount" label="预留限额:" style="margin-left:200px;">
        <Input placeholder="请输入" v-model="formItem.dayResAmount" :disabled="allMark" style="width: 200px"></Input>
      </FormItem>

      <FormItem prop="tenor" label="期限范围:" style="margin-left:150px;">
        <Input v-model="formItem.tenor" placeholder="请输入" :disabled="allMark" style="width: 200px"></Input>
      </FormItem>
      <FormItem style="margin-left:-90px;">
        <Button type="primary" @click="addTerm" size="small" :disabled="allMark">添加</Button>
        <Button type="primary" @click="reset" size="small" :disabled="allMark"  style="margin-left: 10px;">重置</Button>
      </FormItem>
      <br>
      <FormItem>
        <Row type="flex" align="middle">
          <Col span="24">
          <Tag v-for="(item,index) in tenorNameList" :key="index">{{item}}</Tag>
          </Col>
        </Row>
      </FormItem>
      <br>
      <Row type="flex" v-if="type=='2'" align="middle" style="margin-top:35px;">
        <Col span="8"></Col>
        <Col span="2" style="text-align:center">
        <Button type="primary" @click="conf('edit')" size="small">确认</Button>
        </Col>
        <Col span="2">
        <Button type="primary" @click="handleGoBack" size="small">列表</Button>
        </Col>
        <Col span="6">
        </Col>
      </Row>
      <Row type="flex" v-if="type=='3'" align="middle" style="margin-top:35px;">
        <Col span="8"></Col>
        <Col span="2" style="text-align:center">
        <Button type="primary" @click="conf('add')" size="small">确认</Button>
        </Col>
        <Col span="2">
        <Button type="primary" @click="handleGoBack" size="small">列表</Button>
        </Col>
        <Col span="6">
        </Col>
      </Row>
      <Row v-if="type=='1'" type="flex" align="middle" style="margin-top:50px;">
        <Col span="6"></Col>
        <Col span="6" style="text-align:center">
        <Button type="primary" @click="handleRouterGo" size="small">编辑</Button>
        </Col>
        <Col span="6">
        <Button type="primary" @click="handleGoBack" size="small">列表</Button>
        </Col>
        <Col span="6">
        </Col>
      </Row>
    </Form>
  </Card>
</template>

<script>

export default {
  data () {
    return {
      allMark: false,
      gdh: '选项2',
      fundParam: {}, // 资金源对象参数
      fundLoanTypeList: [
        {
          value: 'A',
          label: '自动'
        },
        {
          value: 'P',
          label: '人工'
        }
      ],

      bankNameList: [ ],
      fundSourceAttributeList: [
        {
          value: 'A0',
          label: 'P2P'
        },
        {
          value: 'A1',
          label: '小贷公司'
        },
        {
          value: 'A2',
          label: '银行'
        }
      ],
      statusList: [
        {
          value: '0',
          label: '正常'
        },
        {
          value: '1',
          label: '失效'
        }
      ],
      tenorNameList: [],
      fundCode: '',
      tenorList: [],
      formItem: {
        fundLoanType: '',
        dayLimit: '',
        addMaxAmount: '',
        dayResAmount: '',
        minAmount: '',
        maxAmount: '',
        tenor: '',
        type: '1',
        bankCodeList: [],
        fundName: '',
        fundSourceMobile: '',
        fundSourceIcNo: '',
        fundSourceAttribute: '',
        fundSourceAttributeName: '',
        status: ''
      },
      formValid: {
        fundName: [
          { required: true, message: '资金源名称不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '资金源状态不能为空', trigger: 'blur' }
        ],
        fundSourceAttribute: [
          { required: true, message: '资金源属性不能为空', trigger: 'blur' }
        ],
        fundSourceMobile: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          { pattern: this.$util.regExpMobile, message: '手机号码输入格式不正确', trigger: 'blur' }
        ],
        fundSourceIcNo: [
          { required: true, message: '身份证号码不能为空', trigger: 'blur' },
          { pattern: this.$util.regExpIdCardNo, message: '身份证号码输入格式不正确', trigger: 'blur' }
        ],
        dayLimit: [
          { pattern: this.$util.regExpAmount, message: '当日限额不能为非数字', trigger: 'blur' }
        ],
        addMaxAmount: [
          { pattern: this.$util.regExpAmount, message: '进件最大额度不能为非数字', trigger: 'blur' }
        ],
        minAmount: [
          { pattern: this.$util.regExpAmount, message: '产品金额范围不能为非数字', trigger: 'blur' }
        ],
        maxAmount: [
          { pattern: this.$util.regExpAmount, message: '产品金额范围不能为非数字', trigger: 'blur' }
        ],
        dayResAmount: [
          { pattern: this.$util.regExpAmount, message: '预留限额不能为非数字', trigger: 'blur' }
        ],
        tenor: [
          { pattern: this.$util.regExpNum, message: '预留限额不能为非数字', trigger: 'blur' }
        ],
        fundLoanType: [
          { required: true, message: '放款方式不能为空', trigger: 'blur' }
        ]
      },
      mark: false
    }
  },
  created () {
    var vm = this
    this.$ajax = this.$util.ajax(this.$appContext.marvels_core_financial)
    vm.type = this.$route.query.type
    this.create()
  },
  computed: {},
  mounted () { },
  methods: {
    create () {
      var vm = this
      if (vm.type === '2' || vm.type === '1') {
        if (vm.type === '1') {
          vm.allMark = true
        }
        vm.fundParam = JSON.parse(this.$route.query.fundParam) // 保存参数
        vm.fundCode = vm.fundParam.fundCode
        vm.formItem.fundName = vm.fundParam.fundName
        vm.formItem.fundSourceMobile = vm.fundParam.fundSourceMobile
        vm.formItem.fundSourceIcNo = vm.fundParam.fundSourceIcNo
        this.$ajax.post(this.$appContext.marvels_core_financial + 'financial/fund/service/searchBankAndLimit', { fundCode: vm.fundCode })
          .then(re => {
            if (re.data.code === 'success') {
              var object = re.data.data
              vm.bankNameList = re.data.data.mcfeSupBankList
              console.log('bankNameList', vm.bankNameList)
              vm.tenorList = []
              vm.tenorNameList = []
              vm.formItem.bankCodeList = []
              let list = []
              if (object.mcfeFundLimitDto && object.mcfeFundLimitDto != null && object.mcfeFundLimitDto.tenorList && object.mcfeFundLimitDto.tenorList != null) {
                vm.tenorList = object.mcfeFundLimitDto.tenorList
                for (var i = 0; i < vm.tenorList.length; i++) {
                  list.push(vm.tenorList[i] + '期')
                }
              }
              vm.tenorNameList = list
              // 加载资金源授权银行
              var mcfeFundBankDtoList = object.mcfeFundBankDtoList
              for (var j = 0; j < mcfeFundBankDtoList.length; j++) {
                vm.formItem.bankCodeList.push(mcfeFundBankDtoList[j].bankCode)
              }
              console.log('bankCodeList', vm.formItem.bankCodeList)
              vm.formItem.dayLimit = object.mcfeFundLimitDto.dayLimit
                ? object.mcfeFundLimitDto.dayLimit + ''
                : '0'
              vm.formItem.addMaxAmount = object.mcfeFundLimitDto.addMaxAmount
                ? object.mcfeFundLimitDto.addMaxAmount + ''
                : '0'
              vm.formItem.dayResAmount = object.mcfeFundLimitDto.dayResAmount
                ? object.mcfeFundLimitDto.dayResAmount + ''
                : '0'
              vm.formItem.minAmount = object.mcfeFundLimitDto.minAmount
                ? object.mcfeFundLimitDto.minAmount + ''
                : '0'
              vm.formItem.maxAmount = object.mcfeFundLimitDto.maxAmount
                ? object.mcfeFundLimitDto.maxAmount + ''
                : '0'
            } else if (re.data.code === 'error') {
              this.$Message.warning(re.data.data)
            }
          })
          .catch(er => { })
        vm.mark = false
        if (vm.fundParam.status === '正常') {
          vm.formItem.status = '0'
        } else if (vm.fundParam.status === '失效') {
          vm.formItem.status = '1'
        }
        if (vm.fundParam.fundLoanType === '人工') {
          vm.formItem.fundLoanType = 'P'
        } else if (vm.fundParam.fundLoanType === '自动') {
          vm.formItem.fundLoanType = 'A'
        }
        vm.formItem.fundSourceAttribute = vm.fundParam.fundSourceAttribute
      } else if (vm.type === '3') {
        this.$ajax.post(this.$appContext.marvels_core_financial + 'financial/fund/service/searchBankAndLimit', { fundCode: vm.fundCode })
          .then(re => {
            if (re.data.code === 'success') {
              vm.bankNameList = re.data.data.mcfeSupBankList
            }
          }).catch(er => { })
      }
    },

    // 重置期数
    reset () {
      var vm = this
      vm.tenorList = []
      vm.tenorNameList = []
      vm.formItem.tenor = ''
    },
    // 添加期数
    addTerm () {
      var vm = this
      var r = /^\+?[1-9][0-9]*$/
      if (!r.test(vm.formItem.tenor)) {
        this.$Message.warning('输入期数不正确')
        return
      }
      for (var i = 0; i < vm.tenorList.length; i++) {
        if (vm.tenorList[i] === vm.formItem.tenor) {
          this.$Message.warning('期数已经存在')
          return
        }
      }
      vm.tenorList.push(vm.formItem.tenor)
      vm.tenorNameList.push(vm.formItem.tenor + '期')
    },

    handleRouterGo () {
      var vm = this
      vm.type = '2'
      vm.allMark = false
      this.create()
    },
    editConf () {

    },
    conf (judge) {
      this.$refs.formItem.validate(valid => {
        if (valid) {
          if (!confirm('确认提交?')) {
            return
          }
          var vm = this

          // 获取资金源属性名称和code
          for (var i = 0; i < vm.fundSourceAttributeList.length; i++) {
            if (vm.fundSourceAttributeList[i].value === vm.formItem.fundSourceAttribute) {
              vm.formItem.fundSourceAttributeName = vm.fundSourceAttributeList[i].label
            }
          }
          var mcfeFundBankDtoList = []
          // 获取选择的银行名称和code
          for (var j = 0; j < vm.bankNameList.length; j++) {
            if (vm.formItem.bankCodeList.indexOf(vm.bankNameList[j].bankCode) !== -1) {
              var mcfeFundBankDto = {
                bankName: vm.bankNameList[j].bankName,
                bankCode: vm.bankNameList[j].bankCode
              }
              mcfeFundBankDtoList.push(mcfeFundBankDto)
            }
          }

          // 生成资金源限额对象
          var mcfeFundLimitDto = {
            dayLimit: vm.formItem.dayLimit,
            addMaxAmount: vm.formItem.addMaxAmount,
            dayResAmount: vm.formItem.dayResAmount,
            minAmount: vm.formItem.minAmount,
            maxAmount: vm.formItem.maxAmount,
            tenorList: vm.tenorList
          }
          var param = {
            status: vm.formItem.status,
            fundSourceAttribute: vm.formItem.fundSourceAttribute,
            fundSourceAttributeName: vm.formItem.fundSourceAttributeName,
            fundName: vm.formItem.fundName,
            fundSourceMobile: vm.formItem.fundSourceMobile,
            fundSourceIcNo: vm.formItem.fundSourceIcNo,
            fundLoanType: vm.formItem.fundLoanType,
            mcfeFundBankDtoList: mcfeFundBankDtoList,
            mcfeFundLimitDto: mcfeFundLimitDto,
            judge: judge,
            fundCode: vm.fundCode
          }
          console.log(param)
          this.$ajax.post(this.$appContext.marvels_core_financial + 'financial/fund/service/addFundSource', param)
            .then(re => {
              if (re.data.code === 'success') {
                this.$Message.success(re.data.data)
                this.$router.push({ name: 'fundSource' })
              } else {
                this.$Message.warning(re.data.data)
              }
            })
            .catch(er => { })
        }
      })
    },
    handleGoBack () {
      this.$router.push({ name: 'fundSource' })
    }
  },
  props: {},
  watch: {
    $route (val) { }
  }
}
</script>

<style lang="less">
.fund_see {
}
.check {
  margin-bottom: 20px;
}
#tagContainer {
  margin-top: 10px;
}
</style>
